<?php

namespace app\common\model\banxi\learn;

use app\common\enums\ReviewTypeEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 上课记录
 */
class LearnTwentyAntiForgettingReview extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_twenty_anti_forgetting_review';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'reviewTypeCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getReviewTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['reviewType'] ?? '');
        return (new ReviewTypeEnum())->getDescription($value) ?? '';
    }


    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------


    public function getByAntiForgettingId($antiForgettingId, $reviewType)
    {
        return $this->with([
            'forgetWords' => function ($query) {
                $query->where(['result' => 3]);
            }])->where([
            'antiAmnesiaId' => $antiForgettingId,
            'isSubmit' => 1,
            'reviewType' => $reviewType,
        ])->order(['id' => 'desc'])->find();
    }




    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function forgetWords()
    {
        return $this->hasMany(LearnTwentyAntiForgettingReviewWord::class, 'reviewId', 'id');
    }

}